package com.zhengke.controller.archie;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSONArray;
import com.zhengke.common.util.tools.LoadProperties;
import com.zhengke.pojo.archie.HelpCenter;
import com.zhengke.pojo.archie.HelpDetail;
import com.zhengke.service.archie.HelpCenterService;
/**
 * @author 黄保富
 * @date 2020年1月13日  
 * @version 1.0.0 
 * 
 */
@Controller
public class HelpCenterController {
	
	//注入service层的对象
	@Autowired
	HelpCenterService helpService;
	
	@RequestMapping("toHelpList.do")
	public String gotoHelpListjsp(ModelMap model) {
		model.put("tem",new Date());
		return "tv/helpCenter/helpList";//带的有斜杆
	}
	
	/**
	 * 查询板块的控制层
	 */
	@RequestMapping("querryBrand.do")
	public void getHelpData(HttpServletResponse response,HttpServletRequest request) {
		try {
		//获取response对象
		List<HelpCenter> helpCenterList = helpService.querryAllHelpCenter();//查询板块对应的集合
		//需要的信息内容返回到前台中
		List<Map<String,String>> helpList = new ArrayList<Map<String,String>>();
		//从配置文件中获取到当没有上传图片的时候显示的图片地址
		String notFoundPic = LoadProperties.findValue("losePicForHelpList");
		//遍历集合进行数据的添加
		for(HelpCenter help :  helpCenterList) {
 			Map<String,String> map = new HashMap<String, String>();
			//获取得到的对应的图片的地址信息
			String picPath = help.getHelp_picPath();
			//判断是否为空值，如果为空就赋值默认的值
			if(picPath == null || "".equals(picPath)) {
				//将如果为空就将默认的图片的地址信息返回过去
				picPath = request.getSession().getServletContext().getRealPath(notFoundPic);
			}else {
				//判断如果不为空的话，就创在项目中的创建一个相对的文件夹进行保存
				File f = new File(picPath);
				if(!f.exists()) {
					picPath = request.getSession().getServletContext().getRealPath(notFoundPic);
				}
			}
			//将信息放到map集合里面返回到前台
			map.put("picPath", picPath);
			map.put("helpName",help.getHelp_name());
			map.put("helpId", help.getHelp_id());
			helpList.add(map);
		    }
		response.setCharacterEncoding("utf-8");
		response.setHeader("Pragma","No-cache"); 
		response.setHeader("Cache-Control","no-cache"); 
		response.setDateHeader("Expires", 0);  
		response.getWriter().print("var dataList="+JSONArray.toJSON(helpList));	
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 跳转到帮助中心对应板块的详细的页面的方法
	 */
	@RequestMapping("toHelpDetail.do")
	public String toHelpList(String helpId,ModelMap model) {
		//将传过来的helpId值传到跳转的页面中去
		model.put("helpId",helpId);
		//传递的时候顺便传递一个当前的时间值避免缓存
		model.put("tmp",new Date());
		return "tv/helpCenter/helpDetail";
	}
	
	
	/**
	 * 跳转到帮助中心之后请求的值的接口的方法
	 * 就是查询对应的父级帮助中心的对应的二级标题的图片信息
	 * 参数应该是对应的父类的标题的id
	 */
	@RequestMapping("helpDetail.do")
	public void getDataDetailMethod(String helpId,HttpServletRequest request,HttpServletResponse response) {
		try {
			//获取配置文件中的默认的图片信息
			String notFoundPic =  LoadProperties.findValue("losePicForHelpDetail");
			List<HelpDetail> detailList = helpService.querryDetailByHelpId(helpId);
			List<String> picList = new ArrayList<String>();
			for(HelpDetail helpDetail : detailList) {
				String picPath = helpDetail.getHelp_picPath();
				if(picPath == null || "".equals(picPath)) {
					//将如果为空就将默认的图片的地址信息返回过去
					picPath = request.getSession().getServletContext().getRealPath(notFoundPic);
				}else {
					//判断如果不为空的话，就创在项目中的创建一个相对的文件夹进行保存
					File f = new File(picPath);
					if(!f.exists()) {
						picPath = request.getSession().getServletContext().getRealPath(notFoundPic);
					}
				}
				picList.add(picPath);
			}
			Map<String,Object> temp = new HashMap<String, Object>();
			temp.put("images",picList);
			temp.put("total",picList.size());
			response.setCharacterEncoding("utf-8");
			response.setHeader("Pragma","No-cache"); 
			response.setHeader("Cache-Control","no-cache"); 
			response.setDateHeader("Expires", 0);  
			response.getWriter().print("var dataList="+	JSONUtils.toJSONString(temp));	
		}catch(Exception e) {
			e.getStackTrace();
		}
	}
	
}
